package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.FreemarkerUtil;
import cn.wolfcode.trip.base.domain.Goods;
import cn.wolfcode.trip.base.domain.GoodsType;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.GoodsQueryObject;
import cn.wolfcode.trip.base.query.GoodsTypeQueryObject;
import cn.wolfcode.trip.base.service.IGoodsService;
import cn.wolfcode.trip.base.service.IGoodsTypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IGoodsTypeService goodsTypeService;
    @Autowired
    private ServletContext ctx;

    /*
        查询所有推荐的商品
        资源: /goods/commends
        动作: GET
        参数: state=true
     */
    @GetMapping("/commends")
    public Object getGoodsCommends(GoodsQueryObject qo){
        List<Goods> list = goodsService.selectForList(qo).getList();
        return list;
    }

    /*
        查询某个类别的商品
        资源: /goods
        动作: GET
        参数: type=x
     */
    @GetMapping()
    public void getGoodsOfType(GoodsQueryObject qo , HttpServletResponse resp) throws Exception {
        resp.setContentType("text/html;charset=utf-8");
        String dir = ctx.getRealPath("/template");
        qo.setPageSize(0);
        Map<String, Object> map = new HashMap<>();
        PageInfo<Goods> info = goodsService.selectForList(qo);
        map.put("info", info);
        map.put("id",qo.getType());
        FreemarkerUtil.wirteTempDate(dir, "goodsClassify.ftl", map, resp.getWriter());
    }

    /*
        查询所有启用商品的类型
        资源:/goods/types
        动作:GET
        参数:state=1
     */
    @GetMapping("/types")
    public Object getTypes(@RequestParam("state") Integer state)  {
        List<GoodsType> list = goodsTypeService.listForState(state);
        return list;
    }

    /*
        查询某个商品
        资源:/goods/{id}
        动作:GET
        参数:
     */
    @GetMapping("/{id}")
    public Object getGoods(@PathVariable Long id)  {
        Goods goods = goodsService.getGoods(id);
        return goods;
    }


}
